package com.accelstack.cmp.init;

import com.accelstack.cmp.dto.ServerWithComponentsRequest;
import com.accelstack.cmp.entity.*;
import com.accelstack.cmp.enums.DiskType;
import com.accelstack.cmp.enums.ServerStatus;
import com.accelstack.cmp.enums.UserStatus;
import com.accelstack.cmp.repository.DatacenterRepository;
import com.accelstack.cmp.repository.GpuModelRepository;
import com.accelstack.cmp.repository.RackRepository;
import com.accelstack.cmp.repository.ServerRepository;
import com.accelstack.cmp.repository.UserRepository;
import com.accelstack.cmp.service.ServerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 数据初始化器
 * 在应用启动时创建默认管理员用户
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {
    
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;
    private final GpuModelRepository gpuModelRepository;
    private final DatacenterRepository datacenterRepository;
    private final RackRepository rackRepository;
    private final ServerRepository serverRepository;
    private final ServerService serverService;
    
    @Override
    public void run(String... args) {
        // 初始化管理员用户
        if (!userRepository.existsByUsername("admin")) {
            this.initAdminUser();
        } else {
            log.info("管理员用户已存在，跳过初始化");
        }
        
        // 初始化GPU型号
        if (gpuModelRepository.count() == 0) {
            this.initGpuModels();
        } else {
            log.info("GPU型号数据已存在，跳过初始化");
        }
        
        // 初始化数据中心和机架
        if (datacenterRepository.count() == 0) {
            this.initDatacenterAndRacks();
        } else {
            log.info("数据中心数据已存在，跳过初始化");
        }
        
        // 初始化服务器
        if (serverRepository.count() == 0) {
            this.initServers();
        } else {
            log.info("服务器数据已存在，跳过初始化");
        }
    }
    
    /**
     * 初始化管理员用户
     */
    private void initAdminUser() {
        User admin = new User();
        admin.setUsername("admin");
        admin.setEmail("admin@tongjie.com");
        admin.setAccessKey("AKADMIN1234567890ABCDEFGHIJ");
        admin.setSecretKey(passwordEncoder.encode("admin_secret_key_123456"));
        admin.setStatus(UserStatus.ACTIVE);
        admin.setDescription("系统管理员");
        admin.setCreatedAt(new Date());
        admin.setCreatedBy("system");
        
        userRepository.save(admin);
        
        log.info("========================================");
        log.info("默认管理员用户已创建:");
        log.info("  用户名: admin");
        log.info("  Access Key: AKADMIN1234567890ABCDEFGHIJ");
        log.info("  Secret Key: admin_secret_key_123456");
        log.info("  请在生产环境中立即修改默认密钥！");
        log.info("========================================");
    }
    
    /**
     * 初始化GPU型号数据 - Nvidia主流型号
     */
    private void initGpuModels() {
        log.info("开始初始化GPU型号数据...");
        Date now = new Date();
        
        // ========== 数据中心级GPU ==========
        
        // NVIDIA H100
        GpuModel h100 = new GpuModel();
        h100.setName("H100 PCIe");
        h100.setVendor("NVIDIA");
        h100.setVramGb(80);
        h100.setCudaCores(16896);
        h100.setTensorCores(528);
        h100.setMemoryBandwidthGBs(new BigDecimal("2000"));
        h100.setTdpWatts(350);
        h100.setInterfaceType("PCIe 5.0 x16");
        h100.setDescription("NVIDIA最新旗舰数据中心GPU，基于Hopper架构，专为AI和HPC设计");
        h100.setCreatedAt(now);
        gpuModelRepository.save(h100);
        
        // NVIDIA H100 SXM
        GpuModel h100sxm = new GpuModel();
        h100sxm.setName("H100 SXM5");
        h100sxm.setVendor("NVIDIA");
        h100sxm.setVramGb(80);
        h100sxm.setCudaCores(16896);
        h100sxm.setTensorCores(528);
        h100sxm.setMemoryBandwidthGBs(new BigDecimal("3350"));
        h100sxm.setTdpWatts(700);
        h100sxm.setInterfaceType("SXM5");
        h100sxm.setDescription("NVIDIA H100 SXM5版本，提供更高的功率和性能，支持NVLink");
        h100sxm.setCreatedAt(now);
        gpuModelRepository.save(h100sxm);
        
        // NVIDIA A100 PCIe
        GpuModel a100pcie = new GpuModel();
        a100pcie.setName("A100 PCIe 80GB");
        a100pcie.setVendor("NVIDIA");
        a100pcie.setVramGb(80);
        a100pcie.setCudaCores(6912);
        a100pcie.setTensorCores(432);
        a100pcie.setMemoryBandwidthGBs(new BigDecimal("1935"));
        a100pcie.setTdpWatts(300);
        a100pcie.setInterfaceType("PCIe 4.0 x16");
        a100pcie.setDescription("NVIDIA A100基于Ampere架构，数据中心AI训练和推理的主力GPU");
        a100pcie.setCreatedAt(now);
        gpuModelRepository.save(a100pcie);
        
        // NVIDIA A100 SXM4
        GpuModel a100sxm = new GpuModel();
        a100sxm.setName("A100 SXM4 80GB");
        a100sxm.setVendor("NVIDIA");
        a100sxm.setVramGb(80);
        a100sxm.setCudaCores(6912);
        a100sxm.setTensorCores(432);
        a100sxm.setMemoryBandwidthGBs(new BigDecimal("2039"));
        a100sxm.setTdpWatts(500);
        a100sxm.setInterfaceType("SXM4");
        a100sxm.setDescription("NVIDIA A100 SXM4版本，支持NVLink互联，适合大规模训练");
        a100sxm.setCreatedAt(now);
        gpuModelRepository.save(a100sxm);
        
        // NVIDIA A100 40GB
        GpuModel a10040 = new GpuModel();
        a10040.setName("A100 PCIe 40GB");
        a10040.setVendor("NVIDIA");
        a10040.setVramGb(40);
        a10040.setCudaCores(6912);
        a10040.setTensorCores(432);
        a10040.setMemoryBandwidthGBs(new BigDecimal("1555"));
        a10040.setTdpWatts(250);
        a10040.setInterfaceType("PCIe 4.0 x16");
        a10040.setDescription("NVIDIA A100 40GB版本，适合中等规模AI工作负载");
        a10040.setCreatedAt(now);
        gpuModelRepository.save(a10040);
        
        // NVIDIA A40
        GpuModel a40 = new GpuModel();
        a40.setName("A40");
        a40.setVendor("NVIDIA");
        a40.setVramGb(48);
        a40.setCudaCores(10752);
        a40.setTensorCores(336);
        a40.setMemoryBandwidthGBs(new BigDecimal("696"));
        a40.setTdpWatts(300);
        a40.setInterfaceType("PCIe 4.0 x16");
        a40.setDescription("多功能数据中心GPU，适合虚拟工作站、渲染和AI推理");
        a40.setCreatedAt(now);
        gpuModelRepository.save(a40);
        
        // NVIDIA A30
        GpuModel a30 = new GpuModel();
        a30.setName("A30");
        a30.setVendor("NVIDIA");
        a30.setVramGb(24);
        a30.setCudaCores(3584);
        a30.setTensorCores(224);
        a30.setMemoryBandwidthGBs(new BigDecimal("933"));
        a30.setTdpWatts(165);
        a30.setInterfaceType("PCIe 4.0 x16");
        a30.setDescription("主流数据中心GPU，平衡性能和功耗，适合AI推理");
        a30.setCreatedAt(now);
        gpuModelRepository.save(a30);
        
        // NVIDIA A10
        GpuModel a10 = new GpuModel();
        a10.setName("A10");
        a10.setVendor("NVIDIA");
        a10.setVramGb(24);
        a10.setCudaCores(9216);
        a10.setTensorCores(288);
        a10.setMemoryBandwidthGBs(new BigDecimal("600"));
        a10.setTdpWatts(150);
        a10.setInterfaceType("PCIe 4.0 x16");
        a10.setDescription("紧凑型数据中心GPU，适合虚拟桌面、AI推理和图形渲染");
        a10.setCreatedAt(now);
        gpuModelRepository.save(a10);
        
        // NVIDIA V100
        GpuModel v100 = new GpuModel();
        v100.setName("V100 PCIe 32GB");
        v100.setVendor("NVIDIA");
        v100.setVramGb(32);
        v100.setCudaCores(5120);
        v100.setTensorCores(640);
        v100.setMemoryBandwidthGBs(new BigDecimal("900"));
        v100.setTdpWatts(250);
        v100.setInterfaceType("PCIe 3.0 x16");
        v100.setDescription("基于Volta架构的经典数据中心GPU，首次引入Tensor Cores");
        v100.setCreatedAt(now);
        gpuModelRepository.save(v100);
        
        // NVIDIA T4
        GpuModel t4 = new GpuModel();
        t4.setName("T4");
        t4.setVendor("NVIDIA");
        t4.setVramGb(16);
        t4.setCudaCores(2560);
        t4.setTensorCores(320);
        t4.setMemoryBandwidthGBs(new BigDecimal("300"));
        t4.setTdpWatts(70);
        t4.setInterfaceType("PCIe 3.0 x16");
        t4.setDescription("低功耗AI推理GPU，基于Turing架构，适合边缘部署");
        t4.setCreatedAt(now);
        gpuModelRepository.save(t4);
        
        // ========== 工作站/专业级GPU ==========
        
        // NVIDIA RTX 6000 Ada Generation
        GpuModel rtx6000ada = new GpuModel();
        rtx6000ada.setName("RTX 6000 Ada Generation");
        rtx6000ada.setVendor("NVIDIA");
        rtx6000ada.setVramGb(48);
        rtx6000ada.setCudaCores(18176);
        rtx6000ada.setTensorCores(568);
        rtx6000ada.setMemoryBandwidthGBs(new BigDecimal("960"));
        rtx6000ada.setTdpWatts(300);
        rtx6000ada.setInterfaceType("PCIe 4.0 x16");
        rtx6000ada.setDescription("基于Ada Lovelace架构的旗舰专业显卡，支持第三代RT Cores");
        rtx6000ada.setCreatedAt(now);
        gpuModelRepository.save(rtx6000ada);
        
        // NVIDIA RTX 4090
        GpuModel rtx4090 = new GpuModel();
        rtx4090.setName("RTX 4090");
        rtx4090.setVendor("NVIDIA");
        rtx4090.setVramGb(24);
        rtx4090.setCudaCores(16384);
        rtx4090.setTensorCores(512);
        rtx4090.setMemoryBandwidthGBs(new BigDecimal("1008"));
        rtx4090.setTdpWatts(450);
        rtx4090.setInterfaceType("PCIe 4.0 x16");
        rtx4090.setDescription("消费级旗舰显卡，基于Ada Lovelace架构，强大的AI和渲染性能");
        rtx4090.setCreatedAt(now);
        gpuModelRepository.save(rtx4090);
        
        // NVIDIA RTX 4080
        GpuModel rtx4080 = new GpuModel();
        rtx4080.setName("RTX 4080");
        rtx4080.setVendor("NVIDIA");
        rtx4080.setVramGb(16);
        rtx4080.setCudaCores(9728);
        rtx4080.setTensorCores(304);
        rtx4080.setMemoryBandwidthGBs(new BigDecimal("716"));
        rtx4080.setTdpWatts(320);
        rtx4080.setInterfaceType("PCIe 4.0 x16");
        rtx4080.setDescription("高性能显卡，适合4K游戏和专业内容创作");
        rtx4080.setCreatedAt(now);
        gpuModelRepository.save(rtx4080);
        
        // NVIDIA RTX 4070 Ti
        GpuModel rtx4070ti = new GpuModel();
        rtx4070ti.setName("RTX 4070 Ti");
        rtx4070ti.setVendor("NVIDIA");
        rtx4070ti.setVramGb(12);
        rtx4070ti.setCudaCores(7680);
        rtx4070ti.setTensorCores(240);
        rtx4070ti.setMemoryBandwidthGBs(new BigDecimal("504"));
        rtx4070ti.setTdpWatts(285);
        rtx4070ti.setInterfaceType("PCIe 4.0 x16");
        rtx4070ti.setDescription("主流高性能显卡，平衡性能与功耗");
        rtx4070ti.setCreatedAt(now);
        gpuModelRepository.save(rtx4070ti);
        
        // NVIDIA RTX 4060 Ti
        GpuModel rtx4060ti = new GpuModel();
        rtx4060ti.setName("RTX 4060 Ti");
        rtx4060ti.setVendor("NVIDIA");
        rtx4060ti.setVramGb(16);
        rtx4060ti.setCudaCores(4352);
        rtx4060ti.setTensorCores(136);
        rtx4060ti.setMemoryBandwidthGBs(new BigDecimal("288"));
        rtx4060ti.setTdpWatts(160);
        rtx4060ti.setInterfaceType("PCIe 4.0 x8");
        rtx4060ti.setDescription("主流性能显卡，适合1440p游戏和轻度内容创作");
        rtx4060ti.setCreatedAt(now);
        gpuModelRepository.save(rtx4060ti);
        
        // NVIDIA RTX 4060
        GpuModel rtx4060 = new GpuModel();
        rtx4060.setName("RTX 4060");
        rtx4060.setVendor("NVIDIA");
        rtx4060.setVramGb(8);
        rtx4060.setCudaCores(3072);
        rtx4060.setTensorCores(96);
        rtx4060.setMemoryBandwidthGBs(new BigDecimal("272"));
        rtx4060.setTdpWatts(115);
        rtx4060.setInterfaceType("PCIe 4.0 x8");
        rtx4060.setDescription("主流入门显卡，适合1080p游戏和日常使用");
        rtx4060.setCreatedAt(now);
        gpuModelRepository.save(rtx4060);
        
        // NVIDIA RTX 3090
        GpuModel rtx3090 = new GpuModel();
        rtx3090.setName("RTX 3090");
        rtx3090.setVendor("NVIDIA");
        rtx3090.setVramGb(24);
        rtx3090.setCudaCores(10496);
        rtx3090.setTensorCores(328);
        rtx3090.setMemoryBandwidthGBs(new BigDecimal("936"));
        rtx3090.setTdpWatts(350);
        rtx3090.setInterfaceType("PCIe 4.0 x16");
        rtx3090.setDescription("基于Ampere架构的前代旗舰，24GB显存适合AI开发");
        rtx3090.setCreatedAt(now);
        gpuModelRepository.save(rtx3090);
        
        // NVIDIA RTX 3080 Ti
        GpuModel rtx3080ti = new GpuModel();
        rtx3080ti.setName("RTX 3080 Ti");
        rtx3080ti.setVendor("NVIDIA");
        rtx3080ti.setVramGb(12);
        rtx3080ti.setCudaCores(10240);
        rtx3080ti.setTensorCores(320);
        rtx3080ti.setMemoryBandwidthGBs(new BigDecimal("912"));
        rtx3080ti.setTdpWatts(350);
        rtx3080ti.setInterfaceType("PCIe 4.0 x16");
        rtx3080ti.setDescription("Ampere架构高性能显卡");
        rtx3080ti.setCreatedAt(now);
        gpuModelRepository.save(rtx3080ti);
        
        // NVIDIA RTX 3080
        GpuModel rtx3080 = new GpuModel();
        rtx3080.setName("RTX 3080");
        rtx3080.setVendor("NVIDIA");
        rtx3080.setVramGb(10);
        rtx3080.setCudaCores(8704);
        rtx3080.setTensorCores(272);
        rtx3080.setMemoryBandwidthGBs(new BigDecimal("760"));
        rtx3080.setTdpWatts(320);
        rtx3080.setInterfaceType("PCIe 4.0 x16");
        rtx3080.setDescription("主流高性能显卡，性价比出色");
        rtx3080.setCreatedAt(now);
        gpuModelRepository.save(rtx3080);
        
        // NVIDIA RTX 3070 Ti
        GpuModel rtx3070ti = new GpuModel();
        rtx3070ti.setName("RTX 3070 Ti");
        rtx3070ti.setVendor("NVIDIA");
        rtx3070ti.setVramGb(8);
        rtx3070ti.setCudaCores(6144);
        rtx3070ti.setTensorCores(192);
        rtx3070ti.setMemoryBandwidthGBs(new BigDecimal("608"));
        rtx3070ti.setTdpWatts(290);
        rtx3070ti.setInterfaceType("PCIe 4.0 x16");
        rtx3070ti.setDescription("中端高性能显卡");
        rtx3070ti.setCreatedAt(now);
        gpuModelRepository.save(rtx3070ti);
        
        // NVIDIA L40
        GpuModel l40 = new GpuModel();
        l40.setName("L40");
        l40.setVendor("NVIDIA");
        l40.setVramGb(48);
        l40.setCudaCores(18176);
        l40.setTensorCores(568);
        l40.setMemoryBandwidthGBs(new BigDecimal("864"));
        l40.setTdpWatts(300);
        l40.setInterfaceType("PCIe 4.0 x16");
        l40.setDescription("基于Ada Lovelace架构的通用数据中心GPU，兼顾图形和AI");
        l40.setCreatedAt(now);
        gpuModelRepository.save(l40);
        
        // NVIDIA L4
        GpuModel l4 = new GpuModel();
        l4.setName("L4");
        l4.setVendor("NVIDIA");
        l4.setVramGb(24);
        l4.setCudaCores(7424);
        l4.setTensorCores(232);
        l4.setMemoryBandwidthGBs(new BigDecimal("300"));
        l4.setTdpWatts(72);
        l4.setInterfaceType("PCIe 4.0 x16");
        l4.setDescription("新一代低功耗AI推理GPU，基于Ada Lovelace架构");
        l4.setCreatedAt(now);
        gpuModelRepository.save(l4);
        
        log.info("========================================");
        log.info("GPU型号数据初始化完成！");
        log.info("共初始化 {} 个Nvidia GPU型号", gpuModelRepository.count());
        log.info("包括数据中心级、工作站级和专业级GPU");
        log.info("========================================");
    }
    
    /**
     * 初始化数据中心和机架
     */
    private void initDatacenterAndRacks() {
        log.info("开始初始化数据中心和机架数据...");
        Date now = new Date();
        
        // 创建数据中心
        Datacenter datacenter = new Datacenter();
        datacenter.setName("北京数据中心");
        datacenter.setLocation("北京市海淀区");
        datacenter.setContact("张经理 010-12345678");
        datacenter.setDescription("主数据中心，提供OpenStack虚拟化服务");
        datacenter.setAuthUrl("http://192.168.1.10:5000/v3");
        datacenter.setRegion("RegionOne");
        datacenter.setProject("admin");
        datacenter.setUsername("admin");
        datacenter.setPassword("openstack_admin_pass");
        datacenter.setDomain("Default");
        datacenter.setCreatedAt(now);
        Datacenter savedDc = datacenterRepository.save(datacenter);
        
        log.info("已创建数据中心: {}", savedDc.getName());
        
        // 为数据中心创建机架
        for (int i = 1; i <= 3; i++) {
            Rack rack = new Rack();
            rack.setDatacenter(savedDc);
            rack.setName("机架-A0" + i);
            rack.setUHeight(42);
            rack.setDescription("标准42U机架");
            rackRepository.save(rack);
        }
        
        log.info("已为数据中心创建 3 个机架");
        log.info("========================================");
    }
    
    /**
     * 初始化服务器（主机）数据
     */
    private void initServers() {
        log.info("开始初始化服务器数据...");
        
        // 获取数据中心和机架
        Datacenter datacenter = datacenterRepository.findAll().get(0);
        List<Rack> racks = rackRepository.findByDatacenterId(datacenter.getId());
        Rack rack1 = racks.get(0);
        
        // 获取GPU型号
        List<GpuModel> gpuModels = gpuModelRepository.findAll();
        GpuModel a100 = gpuModels.stream()
                .filter(gm -> "A100 PCIe 80GB".equals(gm.getName()))
                .findFirst()
                .orElse(gpuModels.get(0));
        GpuModel rtx4090 = gpuModels.stream()
                .filter(gm -> "RTX 4090".equals(gm.getName()))
                .findFirst()
                .orElse(gpuModels.get(0));
        
        // ========== 服务器1: AI训练服务器（4x A100） ==========
        ServerWithComponentsRequest server1Req = new ServerWithComponentsRequest();
        
        Server server1 = new Server();
        server1.setHostname("ai-train-01");
        server1.setDatacenter(datacenter);
        server1.setRack(rack1);
        server1.setUPosition(1);
        server1.setStatus(ServerStatus.ACTIVE);
        server1.setModel("Dell PowerEdge R750xa");
        server1.setSerialNumber("DL-AI-001");
        server1.setManagementIp("192.168.1.101");
        server1.setNotes("AI训练专用服务器，配置4块A100 GPU");
        server1Req.setServer(server1);
        
        // CPU
        Cpu cpu1 = new Cpu();
        cpu1.setModel("Intel Xeon Gold 6348");
        cpu1.setCores(28);
        cpu1.setThreads(56);
        cpu1.setFrequencyGhz(new BigDecimal("2.6"));
        server1Req.getCpus().add(cpu1);
        
        Cpu cpu1_2 = new Cpu();
        cpu1_2.setModel("Intel Xeon Gold 6348");
        cpu1_2.setCores(28);
        cpu1_2.setThreads(56);
        cpu1_2.setFrequencyGhz(new BigDecimal("2.6"));
        server1Req.getCpus().add(cpu1_2);
        
        // 内存 - 8x 64GB = 512GB
        for (int i = 0; i < 8; i++) {
            Memory mem = new Memory();
            mem.setCapacityGb(64);
            mem.setType("DDR4");
            mem.setSpeedMhz(3200);
            server1Req.getMemories().add(mem);
        }
        
        // 磁盘
        Disk disk1_1 = new Disk();
        disk1_1.setModel("Samsung PM9A3");
        disk1_1.setCapacityGb(1920);
        disk1_1.setType(DiskType.SSD);
        disk1_1.setInterfaceType("NVMe");
        server1Req.getDisks().add(disk1_1);
        
        Disk disk1_2 = new Disk();
        disk1_2.setModel("Samsung PM9A3");
        disk1_2.setCapacityGb(3840);
        disk1_2.setType(DiskType.SSD);
        disk1_2.setInterfaceType("NVMe");
        server1Req.getDisks().add(disk1_2);
        
        // 网卡
        NetworkInterface nic1_1 = new NetworkInterface();
        nic1_1.setName("ens1f0");
        nic1_1.setMacAddress("00:1A:2B:3C:4D:01");
        nic1_1.setIpAddress("192.168.1.101");
        nic1_1.setSpeedGbps(new BigDecimal("25"));
        server1Req.getNetworkInterfaces().add(nic1_1);
        
        NetworkInterface nic1_2 = new NetworkInterface();
        nic1_2.setName("ens1f1");
        nic1_2.setMacAddress("00:1A:2B:3C:4D:02");
        nic1_2.setIpAddress("192.168.2.101");
        nic1_2.setSpeedGbps(new BigDecimal("25"));
        server1Req.getNetworkInterfaces().add(nic1_2);
        
        // 物理GPU - 4x A100
        for (int i = 0; i < 4; i++) {
            PhysicalGpu gpu = new PhysicalGpu();
            gpu.setGpuModel(a100);
            gpu.setPciAddress(String.format("0000:0%d:00.0", i + 1));
            gpu.setNvidiaSmiIndex(i);
            server1Req.getPhysicalGpus().add(gpu);
        }
        
        Server createdServer1 = serverService.createWithComponents(server1Req);
        log.info("已创建服务器: {} (4x A100)", createdServer1.getHostname());
        
        // ========== 服务器2: 工作站服务器（2x RTX 4090） ==========
        ServerWithComponentsRequest server2Req = new ServerWithComponentsRequest();
        
        Server server2 = new Server();
        server2.setHostname("workstation-01");
        server2.setDatacenter(datacenter);
        server2.setRack(rack1);
        server2.setUPosition(3);
        server2.setStatus(ServerStatus.ACTIVE);
        server2.setModel("HP Z8 G5");
        server2.setSerialNumber("HP-WS-001");
        server2.setManagementIp("192.168.1.102");
        server2.setNotes("高性能工作站，用于3D渲染和AI推理");
        server2Req.setServer(server2);
        
        // CPU
        Cpu cpu2 = new Cpu();
        cpu2.setModel("Intel Xeon W9-3495X");
        cpu2.setCores(56);
        cpu2.setThreads(112);
        cpu2.setFrequencyGhz(new BigDecimal("1.9"));
        server2Req.getCpus().add(cpu2);
        
        // 内存 - 4x 32GB = 128GB
        for (int i = 0; i < 4; i++) {
            Memory mem = new Memory();
            mem.setCapacityGb(32);
            mem.setType("DDR5");
            mem.setSpeedMhz(4800);
            server2Req.getMemories().add(mem);
        }
        
        // 磁盘
        Disk disk2_1 = new Disk();
        disk2_1.setModel("Samsung 980 PRO");
        disk2_1.setCapacityGb(2000);
        disk2_1.setType(DiskType.SSD);
        disk2_1.setInterfaceType("NVMe");
        server2Req.getDisks().add(disk2_1);
        
        // 网卡
        NetworkInterface nic2 = new NetworkInterface();
        nic2.setName("eth0");
        nic2.setMacAddress("00:1A:2B:3C:5D:01");
        nic2.setIpAddress("192.168.1.102");
        nic2.setSpeedGbps(new BigDecimal("10"));
        server2Req.getNetworkInterfaces().add(nic2);
        
        // 物理GPU - 2x RTX 4090
        for (int i = 0; i < 2; i++) {
            PhysicalGpu gpu = new PhysicalGpu();
            gpu.setGpuModel(rtx4090);
            gpu.setPciAddress(String.format("0000:0%d:00.0", i + 1));
            gpu.setNvidiaSmiIndex(i);
            server2Req.getPhysicalGpus().add(gpu);
        }
        
        Server createdServer2 = serverService.createWithComponents(server2Req);
        log.info("已创建服务器: {} (2x RTX 4090)", createdServer2.getHostname());
        
        // ========== 服务器3: 通用计算服务器（无GPU） ==========
        ServerWithComponentsRequest server3Req = new ServerWithComponentsRequest();
        
        Server server3 = new Server();
        server3.setHostname("compute-01");
        server3.setDatacenter(datacenter);
        server3.setRack(rack1);
        server3.setUPosition(5);
        server3.setStatus(ServerStatus.ACTIVE);
        server3.setModel("Dell PowerEdge R650");
        server3.setSerialNumber("DL-CPU-001");
        server3.setManagementIp("192.168.1.103");
        server3.setNotes("通用计算服务器，用于CPU密集型任务");
        server3Req.setServer(server3);
        
        // CPU
        Cpu cpu3_1 = new Cpu();
        cpu3_1.setModel("Intel Xeon Gold 6338");
        cpu3_1.setCores(32);
        cpu3_1.setThreads(64);
        cpu3_1.setFrequencyGhz(new BigDecimal("2.0"));
        server3Req.getCpus().add(cpu3_1);
        
        Cpu cpu3_2 = new Cpu();
        cpu3_2.setModel("Intel Xeon Gold 6338");
        cpu3_2.setCores(32);
        cpu3_2.setThreads(64);
        cpu3_2.setFrequencyGhz(new BigDecimal("2.0"));
        server3Req.getCpus().add(cpu3_2);
        
        // 内存 - 16x 32GB = 512GB
        for (int i = 0; i < 16; i++) {
            Memory mem = new Memory();
            mem.setCapacityGb(32);
            mem.setType("DDR4");
            mem.setSpeedMhz(3200);
            server3Req.getMemories().add(mem);
        }
        
        // 磁盘
        Disk disk3_1 = new Disk();
        disk3_1.setModel("Intel D7-P5510");
        disk3_1.setCapacityGb(3840);
        disk3_1.setType(DiskType.SSD);
        disk3_1.setInterfaceType("NVMe");
        server3Req.getDisks().add(disk3_1);
        
        for (int i = 0; i < 4; i++) {
            Disk disk = new Disk();
            disk.setModel("Seagate Exos X18");
            disk.setCapacityGb(18000);
            disk.setType(DiskType.HDD);
            disk.setInterfaceType("SATA");
            server3Req.getDisks().add(disk);
        }
        
        // 网卡
        NetworkInterface nic3_1 = new NetworkInterface();
        nic3_1.setName("ens1f0");
        nic3_1.setMacAddress("00:1A:2B:3C:6D:01");
        nic3_1.setIpAddress("192.168.1.103");
        nic3_1.setSpeedGbps(new BigDecimal("25"));
        server3Req.getNetworkInterfaces().add(nic3_1);
        
        NetworkInterface nic3_2 = new NetworkInterface();
        nic3_2.setName("ens1f1");
        nic3_2.setMacAddress("00:1A:2B:3C:6D:02");
        nic3_2.setSpeedGbps(new BigDecimal("25"));
        server3Req.getNetworkInterfaces().add(nic3_2);
        
        Server createdServer3 = serverService.createWithComponents(server3Req);
        log.info("已创建服务器: {} (无GPU)", createdServer3.getHostname());
        
        log.info("========================================");
        log.info("服务器数据初始化完成！");
        log.info("共初始化 {} 个服务器", serverRepository.count());
        log.info("自动创建了虚拟化主机和虚拟GPU");
        log.info("========================================");
    }
}

